package com.etlmaster.executor.steps.func;

import com.etlmaster.executor.Step;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.boot.EtlMasterExecutorBoot;
import com.etlmaster.executor.utils.DpExecutorDbLogUtils;
import com.etlmaster.executor.utils.StepState;

/**
 * @author Wugf
 * @date 2016-03-08
 */
@StepInstruction("execDp")
public class ExecDpProcStep extends Step{

	private ExecDpProcStepMeta meta;
	public ExecDpProcStep(StepMeta meta) {
		super(meta);
		this.meta=(ExecDpProcStepMeta) meta;
		// TODO Auto-generated constructor stub
	}

	@Override
	protected int handle() {
		EtlMasterExecutorBoot boot = null;
		int flag=StepState.RUNNING_SUCCESSED;
		try {
			String params="-f "+meta.getDpProcName()+" -t "+meta.getDpTaskid()+" "+meta.getDpParam();
			boot=new EtlMasterExecutorBoot();
			if(!boot.execute(params)){
				flag=StepState.RUNNING_FAILED;
			}
		} catch (Exception e) {
			meta.log("ERROR","{}",e.getMessage());
			String user=(String) (meta.getVariable("user")!=null?meta.getVariable("user"):"");
			boolean isIgnore=Boolean.parseBoolean( (String) meta.getVariable("ignore"));

			if(!isIgnore)
			{
				String session_id=!boot.getSession_id().equalsIgnoreCase("")?boot.getSession_id():meta.getSession_id();
				DpExecutorDbLogUtils.logProcEnd(session_id, meta.getDpProcName(), meta.getDpTaskid(),
						meta.getVariable("finishStepId").toString(), "运行失败",user);
			}
			return StepState.RUNNING_FAILED;
		}
		
		return flag;
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}

}
